package com.ideaaedi.commonspring.aop;

import com.ideaaedi.commonspring.lite.antidupli.AntiDuplication;
import com.ideaaedi.commonspring.spi.AntiDuplicateExecutor;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.NonNull;

import java.lang.reflect.Method;

public class AntiDuplicationPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor {
    
    public static final String BEAN_NAME = "antiDuplicationPointcutAdvisor";
    
    public AntiDuplicationPointcutAdvisor(@SuppressWarnings("rawtypes") @NonNull AntiDuplicateExecutor antiDuplicateExecutor) {
        AntiDuplicationAdvice advice = new AntiDuplicationAdvice(antiDuplicateExecutor);
        setAdvice(advice);
        setOrder(advice.getOrder());
    }
    
    /**
     * 定义AntiDuplication注解发现逻辑
     */
    @Override
    public boolean matches(@NonNull Method method, @NonNull Class<?> targetClass) {
        AntiDuplication annotation = AnnotationUtils.getAnnotation(method, AntiDuplication.class);
        return annotation != null;
    }
}
